<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="4">
        <el-input placeholder="请输入项目名称" clearable v-model="queryForm.name"></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="query">搜索</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </el-row>
    <el-row class="header">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="name" label="维修项目名称" />
        <el-table-column prop="hour" label="维修所需工时" />
        <el-table-column prop="money" label="维修基础费用" />
        <el-table-column label="总费用" :formatter="calculate" />
        <el-table-column prop="des" label="维修描述" width="350px" />
        <el-table-column prop="company.companyName" label="所属门店" />
        <el-table-column prop="type" label="服务类型" :formatter="formatType" />
        <el-table-column prop="status" label="维修项目状态" :formatter="formatStatus"/>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" :icon="Edit" circle @click="handleEdit(row)"></el-button>
            <el-button type="danger" :icon="Delete" circle @click="removeById(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 50]"
      layout="prev, pager, next, jumper, sizes, total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :background="true"
    />
  </el-card>
  <Dialog v-model="dialogVisible"
          :dialogTitle="dialogTitle"
          v-if="dialogVisible"
          @query="query"
          :dialogRowValue="dialogRowValue" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Edit, Delete } from '@element-plus/icons-vue'
import { list, remove } from '../../../api/maintain'
import Dialog from './dialog'
import { ElMessageBox, ElMessage } from 'element-plus'

const queryForm = ref({
  name: '',
  type: '1',
  pageNum: 1,
  pageSize: 10
})
const tableData = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = ref('')

async function query() {
  const resp = await list(queryForm.value)
  tableData.value = resp.data.data.list
  total.value = resp.data.data.total
}

// vue组件加载完成执行
onMounted(() => {
  query()
})

function handleCurrentChange(pageNum) {
  queryForm.value.pageNum = pageNum
  query()
}

function handleSizeChange(pageSize) {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  query()
}

const dialogRowValue = ref({})
// 添加
function handleAdd() {
  dialogTitle.value = '添加'
  dialogRowValue.value = {}
  dialogVisible.value = true
}
// 编辑
function handleEdit(row) {
  dialogTitle.value = '编辑'
  dialogRowValue.value = JSON.parse(JSON.stringify(row))
  dialogVisible.value = true
}
// 删除
function removeById(row) {
  ElMessageBox.confirm(
    '是否确认删除?',
    '确认框',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消'
    }
  ).then(async () => {
    await remove(row.id)
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    query()
  }).catch(() => {
    /*
    ElMessage({
      type: 'info',
      message: 'Delete canceled'
    })
     */
  })
}

// 状态转换
function formatStatus(row, column) {
  const status = row[column.property]
  if (status === '1') {
    return '启用'
  } else {
    return '停用'
  }
}
// 服务类型转换
function formatType(row, column) {
  const type = row[column.property]
  if (type === '1') {
    return '维修'
  } else {
    return '保养'
  }
}
// 计算维修工时费
function calculate(row) {
  return row.hour * row.money + '元'
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 16px;
  box-sizing: border-box;
}
::v-deep .el-input__suffix {
  align-items: center;
}
</style>
